<template>
  <el-dialog :title="formData.id?'编辑角色':'添加角色'" :visible="isShow" @close="close">
    <el-form ref="form" :rules="rules" :model="formData">
      <el-form-item label="角色名称" prop="title">
        <el-input v-model="formData.title" />
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="formData.description" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="submit">确定</el-button>
      <el-button @click="close">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { addRoles, Roles, editRoles } from '@/api/roles'
export default {
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        title: '',
        description: ''
      },
      rules: {
        title: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 编辑---获取用户详情 进行数据回显
    async editbtn(id) {
      const res = await Roles(id)
      // 数据回显
      this.formData = res.data
    },
    // 编辑和新增按钮
    async  submit() {
      // 进行表单验证
      this.$refs.form.validate()
      if (this.formData.id) {
        // 编辑
        await editRoles(this.formData)
        this.$message.success('编辑成功')
        // editRples()
      } else {
        // 新增
        await addRoles(this.formData)
        this.$message.success('添加成功')
      }
      // 渲染页面
      this.$emit('updata')
      // 关闭弹窗
      this.close()
    },
    // 关闭
    close() {
      // 关闭弹窗
      this.$emit('update:isShow', false)
      // 清空表单
      this.formData = {
        title: '',
        description: ''
      }
      // 表单重置
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style>

</style>
